/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Ripper
 */
public class LoginPanel extends ClientPanel implements ActionListener
{
    private JTextField usernameTextField;
    private JTextField passwordTextField;
    
    private JButton loginButton;
    private JButton registerButton;
    
    public LoginPanel(ClientWindow parent)
    {
        super(parent);
        setLayout(new BorderLayout());
        
        JPanel amashedPanel = new JPanel();
        {
            FlowLayout fl = new FlowLayout();
            fl.setAlignment(FlowLayout.CENTER);
            fl.setVgap(200);
            amashedPanel.setLayout(fl);

            JPanel centerPanel = new JPanel(new GridLayout(3,2));
            {
                usernameTextField = new JTextField();
                passwordTextField = new JTextField();

                loginButton = new JButton("Login");
                loginButton.addActionListener(this);
                
                usernameTextField.addActionListener(this);
                passwordTextField.addActionListener(this);

                centerPanel.add(new JLabel("username:                      "));
                centerPanel.add(usernameTextField);
                centerPanel.add(new JLabel("password: "));
                centerPanel.add(passwordTextField);
                centerPanel.add(new JLabel(""));
                centerPanel.add(loginButton);
            }
            amashedPanel.add(centerPanel);
        }
        add(amashedPanel, BorderLayout.CENTER);
        
        JPanel bottomPanel = new JPanel(new BorderLayout());
        {
            registerButton = new JButton("          Register          ");
            registerButton.addActionListener(this);
            bottomPanel.add(registerButton, BorderLayout.EAST);
        }
        add(bottomPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == loginButton || e.getSource() == passwordTextField 
                || e.getSource() == usernameTextField)
        {
            String usrnm = usernameTextField.getText();
            String pass = passwordTextField.getText();
            
            getClientWindow().attemptLogin(usrnm, pass);
        }
        else if(e.getSource() == registerButton)
        {
            getClientWindow().gotoRegistrationScreen();
        }
    }
    
}
